/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao.impl;

import java.sql.*;

import dao.CreateObject;
import dao.DBOptionDao;
import dbutil.JdbcUtil;
import entity.Student;
import entity.User;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DBOptionImpl implements DBOptionDao {
    public User findUser(String id) throws SQLException {
        User user = null;
        String sql = "select [User_name], [User_password] from T_user where [User_name]=?";
        Object[] args = {id};
        user = this.select(sql, args, new CreateObject<User>() {
            public User crateObject(ResultSet rs) {
                User u = null;
                u = new User();
                try {
                    u.setUserName(rs.getString("User_name"));
                    u.setUserPwd(rs.getString("User_password"));
                } catch (SQLException ex) {
                    Logger.getLogger(DBOptionImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
                return u;
            }
        });
        return user;
    }

    public int addUser(User user) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        int flag = 0;
        String sql = "insert T_user([User_name], [User_password]) values(?,?)";
        Object[] arg = {user.getUserName(), user.getUserPwd()};
        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql);
            for(int i=0; i<arg.length; i++) {
                ps.setObject(i+1, arg[i]);
            }
            flag = ps.executeUpdate();
            return flag;
        } catch(SQLException e) {
            throw e;
        } finally {
            JdbcUtil.free(null, ps, conn);
        }
    }

    public Student findStudent(String id) throws SQLException {
        Student st = null;
        String sql = "select Student_id, Student_name, Sex, Entry_date, [Address] from T_student_information where Student_id=?";
        Object[] args = {id};
        st = this.select(sql, args, new CreateObject<Student>() {
            public Student crateObject(ResultSet rs) {
                Student s = null;
                try {
                   s = new Student();
                   s.setAddress(rs.getString("Address"));
                   s.setEntry_date(rs.getDate("Entry_date"));
                   s.setId(rs.getString("Student_id"));
                   s.setName(rs.getString("Student_name"));
                   s.setSex(rs.getString("Sex"));
                } catch (SQLException ex) {
                    Logger.getLogger(DBOptionImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
                return s;
            }
        });
        return st;
    }

    private <T>T select(String sql, Object[] args, CreateObject<T> co) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        T t = null;
        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql);
            if(args != null) {
                for(int i=0; i<args.length; i++) {
                    ps.setObject(i+1, args[i]);
                }
            }
            rs = ps.executeQuery();
            if(rs.next()) {
                t = co.crateObject(rs);
            }
            return t;
        } catch(SQLException e) {
            throw e;
        } finally {
            JdbcUtil.free(rs, ps, conn);
        }
    }
}
